import {parseNumber} from "@/utils/NumberUtils";

/**
 * 根据套餐信息获取水票记录
 * @param product 当前商品
 * @param customer 客户信息
 * @param packageInfo 套餐信息
 * @param price 单价
 * @param number 购买套餐数量
 */
const getTicketRecordByPackage = (product: API.IProduct,
                                  customer: API.ICustomer,
                                  packageInfo: API.IProductPackage,
                                  price: number,
                                  number = 1
): API.ITicketRecord => {
  // 1. 初始化套餐信息
  const ticketRecord: API.ITicketRecord = {
    customerId: customer?.id,
    productId: product.id,
    packageId: packageInfo.id,
    transactionType: '0',
    rechargeCount: packageInfo.minNum * number,
    actualCount: packageInfo.minNum * number,
    consumeQuantity: 0,
    amountUnit: price,
    amountPayable: price * packageInfo.minNum * number, // 优惠之前的价格
    amountActual: price * packageInfo.minNum * number, // 实际价格
    giftCount: packageInfo.giftNum,
    returnCash: packageInfo.returnCash || 0, // 返现的价格
    productName: product.productName,
    coverImage: product.coverImage,
    unit: product.unit,
    discountPrice: 0,  // 优惠的金额
    brand: product.brandName,
    modelType: product.modelTypeName,
    needReturn: product.needReturn
  }

  switch (packageInfo.type) {
    case '1': // 满减优惠
      ticketRecord.discountPrice = parseNumber(packageInfo.reduceCash) * number;
      ticketRecord.amountActual = price * packageInfo.minNum * number - ticketRecord.discountPrice;
      break;
    case '2': // 折扣优惠
      ticketRecord.amountActual = price * packageInfo.minNum * number * packageInfo.discountRate;
      ticketRecord.discountPrice = ticketRecord.amountPayable - ticketRecord.amountActual;
      break;
    case '3': // 满送优惠
      ticketRecord.giftCount = parseNumber(packageInfo.giftNum) * number;
      ticketRecord.actualCount = packageInfo.minNum * number + ticketRecord.giftCount;
      break;
    case '4': // 返现优惠
      ticketRecord.returnCash = parseNumber(packageInfo.returnCash) * number;
      break;
    default:
      break;
  }
  return ticketRecord;
}

export {
  getTicketRecordByPackage
}
